import axios from 'axios'
import ls from '@/utils/ls'
import EL from '@/utils/el'

// 检测网站是否已重新打包构建更新
async function install() {
  if (import.meta.env.MODE !== 'test') {
    return
  }
  setTimeout(install, 30e3)
  try {
    const res = await axios.get(`${location.origin}?wh=${vio.rndN(5)}`)
    const btime = res.data.match(/(?<=btime=")[^"]+/)?.[0]
    const lsTime = ls.bTimeArr() || []
    const last = Date.parse(lsTime.at(-1) || 0)
    if (Date.parse(btime) - last > 0) {
      ls.bTimeArr([...lsTime, btime].slice(-10))
      if (
        Date.now() - Date.parse(btime) < 1800e3
        && performance.now() > 3e3
      ) {
        // EL.msgSuc('网站已更新, 请刷新', 0)
      }
    }
  } catch(e) {
    console.error('watchBuildErr::', e.message)
  }
}

export default install
